package com.y.ari.controller;


import cn.hutool.core.util.StrUtil;
import com.y.ari.common.Constants;
import com.y.ari.common.RestResponse;
import com.y.ari.dto.UserDTO;
import com.y.ari.dto.UserFormDto;
import com.y.ari.dto.UserHeadInfoDto;
import com.y.ari.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 用户信息 前端控制器
 * </p>
 *
 * @author yang
 * @since 2023-04-19
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private UserService userService;

    /**
     * 发送手机短信验证码
     *
     * @param
     * @return
     */
    @PostMapping("/sendMsg")
    public RestResponse<String> sendMsg(@RequestParam("phone") String phone) {
        if (StringUtils.isNotEmpty(phone)) {
            String code = userService.sendMsg(phone);
            return RestResponse.success(code);
        }
        return RestResponse.validfail("发送失败");
    }


    /**
     * 注册
     *
     * @param userFormDto
     * @return
     */
    @PostMapping("/signup")
    public RestResponse signUp(@RequestBody UserFormDto userFormDto) {
        boolean b = userService.checkCode(userFormDto.getPhone(), userFormDto.getCode());
        if (!b) {
            return RestResponse.validfail("验证码不正确");
        }
        if (!userFormDto.getPassword().equals(userFormDto.getConfirmPassword())) {
            return RestResponse.validfail("信息错误");
        }
        String token = userService.signUp(userFormDto);
        return RestResponse.success(token, "注册成功");
    }

    /**
     * 修改密码
     *
     * @param userFormDto
     * @return
     */
    @PostMapping("/paw")
    public RestResponse changePaw(@RequestBody UserFormDto userFormDto) {
        boolean b = userService.checkCode(userFormDto.getPhone(), userFormDto.getCode());
        if (!b) {
            return RestResponse.validfail("验证码不正确");
        }
        userService.changePaw(userFormDto);
        return RestResponse.success("修改成功");
    }

    /**
     * 登录
     *
     * @param userFormDto
     * @return
     */
    @PostMapping("/login")
    public RestResponse login(@RequestBody UserFormDto userFormDto) {
        String token = userService.login(userFormDto);
        if (token == null) {
            return RestResponse.validfail("参数不合法");
        }
        return RestResponse.success(token, "登录成功");
    }

    /**
     * 查询用户头信息
     *
     * @return
     */
    @PostMapping("/selHeard")
    public RestResponse selHeadInfo() {
        UserHeadInfoDto userHeadInfoDto = userService.selHeadInfo();
        if (userHeadInfoDto == null) {
            return RestResponse.validfail("登录失败");
        }
        return RestResponse.success(userHeadInfoDto, "获取信息成功");
    }


    /**
     * 查询用户
     *
     * @return
     */
    @PostMapping("/selUser")
    public RestResponse selUser() {
        UserDTO userReq = userService.selUser();
        if (userReq == null) {
            return RestResponse.validfail(Constants.net_err);
        }
        userReq.setPassword(null);
        userReq.setName("***");
        userReq.setIdNumber("*********");
        return RestResponse.success(userReq, "");
    }

    /**
     * 修改用户信息
     *
     * @param userDTO
     * @return
     */
    @PutMapping("/modify")
    public RestResponse modifyUser(@RequestBody UserDTO userDTO) {
        UserDTO userReq = userService.modifyUser(userDTO);
        if (userReq == null) {
            return RestResponse.validfail(Constants.net_err);
        }
        return RestResponse.success(userReq, "修改成功");

    }

    /**
     * 修改用户绑定手机号
     *
     * @param
     * @return
     */
    @PutMapping("/modifyPhone")
    public RestResponse modifyPhone(@RequestBody UserFormDto userFormDto) {
        boolean b = userService.checkCode(userFormDto.getPhone(), userFormDto.getCode());
        if (!b) {
            return RestResponse.validfail("验证码不正确");
        }
        String s = userService.modifyPhone(userFormDto);
        return RestResponse.success(s);

    }


    /**
     * 实名认证
     *
     * @param
     * @return
     */
    @PutMapping("/aut")
    public RestResponse modifyAut(@RequestBody UserDTO userDTO) {
        return userService.modifyAut(userDTO);
    }

    /**
     * 退出登录
     *
     * @param request
     * @return
     */
    @PostMapping("/logout")
    public RestResponse logOut(HttpServletRequest request) {
        // 1.获取请求头中的token
        String token = request.getHeader("authorization");
        if (StrUtil.isBlank(token)) {
            return RestResponse.success("退出成功");
        }
        // 2.基于TOKEN获取redis中的用户
        String key = Constants.LOGIN_USER_KEY + token;
        Boolean aBoolean = redisTemplate.delete(key);
        if (aBoolean) {
            return RestResponse.success("退出成功");
        }
        return RestResponse.success(Constants.net_err);
    }

}

